<?php
namespace App\Http\Controllers\ShopWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\ShopWeb\PublicBusiness;
use App\Http\Controllers\Business\ShopWeb\Auth\ShopWebAuth;
use App\Http\Controllers\Business\ShopWeb\SalesmanManagement\SalesmanManagement;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class SalesmanController extends WebController
{
	//地推注册用户
    public function registerUser() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $result = ShopWebAuth::getShopUsersByShopIds($shopIds);
        $shopUserLists = [
            [
                'shopuserid' => -1,
                'shopusername' => '全部',
                'shopusermobile' => '',
                'shopname' => '',
                'shopid' => '',
                'status' => '',
            ],
            [
                'shopuserid' => 0,
                'shopusername' => '默认',
                'shopusermobile' => '',
                'shopname' => '',
                'shopid' => '',
                'status' => '',
            ]
        ];
        if($result['status'] == 'success') {
            $lists = $result['data'];
            foreach($lists as $list) {
                array_push($shopUserLists, $list);
            }
        }
        $data = [
            'shopuserlists' => $shopUserLists,
        ];
        return view('shop.salesman.register_user', $data);
    }
    //地推注册用户列表
    public function registerUserList() {
        $loginUser = Auth::guard('shopweb')->user();
        $loginUserId = $loginUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $loginUserId);
        if(!$shopIdString) {
            $shopIdString = $loginUser->shopid;
            if (stristr($shopIdString, ',')) {
            } else {
            }
        }
        $shopIds = explode(',', $shopIdString);
        if($this->request->has('shopuserid') && ($this->request->get('shopuserid')!='')) {
            $shopUserId = $this->request->get('shopuserid');
        } else {
            $shopUserId = 0;
        }
        if($this->request->has('starttime') && ($this->request->get('starttime')!='')) {
            $startTime = $this->request->get('starttime');
            $startTime = $startTime . " 00:00:00";
        } else {
            $startTime = '';
        }
        if($this->request->has('lasttime') && ($this->request->get('lasttime')!='')) {
            $lastTime = $this->request->get('lasttime');
            $lastTime = $lastTime . " 23:59:59";
        } else {
            $lastTime = '';
        }
        if($this->request->has('status') && ($this->request->get('status')!='')) {
            $status = $this->request->get('status');
        } else {
            $status = '%';
        }
        if($this->request->has('paginate') && ($this->request->get('paginate')!='')) {
            $paginate = $this->request->get('paginate');
        } else {
            $paginate = 50;
        }
        $result = SalesmanManagement::registerList($shopIds,$shopUserId, $startTime, $lastTime, $status, $paginate);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $lists = $result['data'];
        $data = [
            'lists' => $lists,
            'total' => $lists->total(),
            'lastpage' => $lists->lastPage(),
            'page' => $lists->currentPage(),
        ];
        return $this->responseSuccessData($data);
    }
	//地推订单
    public function salesmanOrder() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $result = ShopWebAuth::getShopUsersByShopIds($shopIds);
        $shopUserLists = [
            [
                'shopuserid' => -1,
                'shopusername' => '全部',
                'shopusermobile' => '',
                'shopname' => '',
                'shopid' => '',
                'status' => '',
            ],
            [
                'shopuserid' => 0,
                'shopusername' => '默认',
                'shopusermobile' => '',
                'shopname' => '',
                'shopid' => '',
                'status' => '',
            ]
        ];
        if($result['status'] == 'success') {
            $lists = $result['data'];
            foreach($lists as $list) {
                array_push($shopUserLists, $list);
            }
        }
        $data = [
            'shopuserlists' => $shopUserLists
        ];

        return view('shop.salesman.salesman_order', $data);
    }
    public function salesmanOrderList() {
        $loginUser = Auth::guard('shopweb')->user();
        $loginUserId = $loginUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $loginUserId);
        if(!$shopIdString) {
            $shopIdString = $loginUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        if($this->request->has('shopuserid') && ($this->request->get('shopuserid')!='')) {
            $shopUserId = $this->request->get('shopuserid');
        } else {
            $shopUserId = 0;
        }
        if($this->request->has('starttime') && ($this->request->get('starttime')!='')) {
            $startTime = $this->request->get('starttime');
            $startTime = $startTime . " 00:00:00";
        } else {
            $startTime = '';
        }
        if($this->request->has('lasttime') && ($this->request->get('lasttime')!='')) {
            $lastTime = $this->request->get('lasttime');
            $lastTime = $lastTime . " 23:59:59";
        } else {
            $lastTime = '';
        }
        if($this->request->has('status') && ($this->request->get('status')!='')) {
            $status = $this->request->get('status');
        } else {
            $status = '%';
        }
        if($this->request->has('paginate') && ($this->request->get('paginate')!='')) {
            $paginate = $this->request->get('paginate');
        } else {
            $paginate = 50;
        }
        $result = SalesmanManagement::orderList($shopIds,$shopUserId, $startTime, $lastTime,$status,$paginate);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $lists = $result['data']['lists'];
        $sum = $result['data']['sum'];
        $data = [
            'lists' => $lists,
            'sum' => $sum,
            'total' => $lists->total(),
            'lastpage' => $lists->lastPage(),
            'page' => $lists->currentPage(),
        ];
        return $this->responseSuccessData($data);
    }

}